from PyQt5.QtWidgets import ( QHBoxLayout, QLabel, QPushButton, QDoubleSpinBox,QLineEdit,QMessageBox)
from components.actiondialog import ActionDialog

class KeyboardTypeDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {"type": "typewrite", "text": "", "interval": 0}

        # 文本输入
        text_layout = QHBoxLayout()
        text_layout.addWidget(QLabel("输入文本:"))
        self.text_edit = QLineEdit(self.action.get("text", ""))
        text_layout.addWidget(self.text_edit)

        # 间隔时间
        interval_layout = QHBoxLayout()
        interval_layout.addWidget(QLabel("间隔时间(秒):"))
        self.interval_spin = QDoubleSpinBox()
        self.interval_spin.setRange(0, 10)
        self.interval_spin.setValue(self.action.get("interval", 0))
        interval_layout.addWidget(self.interval_spin)

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addLayout(text_layout)
        self.layout.addLayout(interval_layout)
        self.layout.addWidget(button)

    def accept(self):
        text = self.text_edit.text()
        if not text:
            QMessageBox.warning(self, "警告", "必须输入文本")
            return

        self.action = {
            "type": "typewrite",
            "text": text,
            "interval": self.interval_spin.value(),
            "desc": f"键盘输入: '{text}' (间隔: {self.interval_spin.value()}秒)"
        }
        super().accept()
